package com.fy.fyspace.controller;

import com.fy.fyspace.common.annotation.GlobalInterceptor;
import com.fy.fyspace.common.result.PageResult;
import com.fy.fyspace.common.result.R;
import com.fy.fyspace.constant.KeysConstant;
import com.fy.fyspace.model.dto.file.FileShareDTO;
import com.fy.fyspace.model.vo.file.FileShareResultVO;
import com.fy.fyspace.model.vo.user.UserSessionVO;
import com.fy.fyspace.service.FileShareService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import static com.fy.fyspace.common.result.R.ok;

@Slf4j
@RestController
@RequestMapping("/share")
@Api(tags = "分享文件接口模块")
public class FileShareController extends BaseController {

    @Autowired
    private FileShareService fileShareService;

    @Resource
    private HttpSession httpSession;

    /**
     * 文件分享
     *
     * @param shareFileDTO
     * @return
     */
    @PostMapping("/shareFile")
    @ApiOperation(value = "分享文件", notes = "创建文件分享")
    @GlobalInterceptor
    public R shareFile(
            @ApiParam(hidden = true) HttpSession httpSession,
            @ApiParam(value = "分享文件参数", required = true)  @RequestBody FileShareDTO shareFileDTO) {
        log.info("shareFile:{}", shareFileDTO);
        UserSessionVO userSessionVO = (UserSessionVO) httpSession.getAttribute(KeysConstant.LOGIN_SESSION_KEY);
        FileShareResultVO fileShareResultVO = fileShareService.fileShare(shareFileDTO, userSessionVO);
        log.info("kkkkkkkk:{}", shareFileDTO);
        return ok(fileShareResultVO);
    }

    @PostMapping("/loadShareList")
    @ApiOperation(value = "获取分享列表", notes = "分页获取用户的分享文件列表")
    @GlobalInterceptor
    public R<PageResult> loadShareList(
            @ApiParam(value = "页码", required = true) String pageNo,
            @ApiParam(value = "每页数量", required = true) String pageSize,
            @ApiParam(value = "文件名称", required = true) String keyword) {
        UserSessionVO userSessionVO = (UserSessionVO) httpSession.getAttribute(KeysConstant.LOGIN_SESSION_KEY);
        PageResult pageResult = fileShareService.fileSharePageQuery(userSessionVO, pageNo, pageSize, keyword);
        log.info("返回数据封装:{}", pageResult);
        return ok(pageResult);
    }

    /**
     * 取消分享
     *
     * @param shareIds
     * @return
     */
    @PostMapping("/cancelShare")
    @ApiOperation("取消分享")
    @GlobalInterceptor
    public R cancelShare(String shareIds) {
        log.info("cancelShare:{}", shareIds);
        fileShareService.cancelShare(shareIds);
        return ok();
    }

}
